﻿using System;
using System.Collections.Generic;
using System.ComponentModel;
using System.Data;
using System.Drawing;
using System.Linq;
using System.Text;
using System.Windows.Forms;
using System.IO;

namespace GetHistoryData
{
    public partial class frmPriceEdit : Form
    {
        string StockCode { get; set; }
        string StockName { get; set; }
        DateTime Date { get; set; }
        double Price { get; set; }

        bool IsAdd { get; set; }

        public frmPriceEdit(string stockName, string stockCode)
        {
            this.StockCode = stockCode;
            this.StockName = stockName;
            this.IsAdd = true;

            InitializeComponent();
        }

        public frmPriceEdit(string stockName, string stockCode, DateTime date, double price)
        {
            this.StockCode = stockCode;
            this.StockName = stockName;
            this.Date = date;
            this.Price = price;

            InitializeComponent();
        }

        private void button2_Click(object sender, EventArgs e)
        {
            this.Close();
        }

        private void button1_Click(object sender, EventArgs e)
        {
            try
            {
                List<LSStockPrice> list = new List<LSStockPrice>();

                if (File.Exists(Application.StartupPath + AppSettings.GetValue<string>("DailyOldPath") + StockCode + ".txt"))
                    File.ReadAllLines(Application.StartupPath + AppSettings.GetValue<string>("DailyOldPath") + StockCode + ".txt").ToList().ForEach(l => list.Add(new LSStockPrice() { Date = Convert.ToDateTime(l.Split(',')[0]), ClosePrice = Convert.ToDouble(l.Split(',')[1]) }));

                Dictionary<DateTime, LSStockPrice> dic = list.ToDictionary(l => l.Date);

                if (!dic.ContainsKey(dateTimePicker1.Value))
                {
                    list.Add(new LSStockPrice() { StockCode = StockCode, Date = dateTimePicker1.Value, ClosePrice = Convert.ToDouble(this.numericUpDown1.Value) });
                }
                else
                {
                    dic[dateTimePicker1.Value].ClosePrice = Convert.ToDouble(this.numericUpDown1.Value);
                }

                StringBuilder sb = new StringBuilder();
                list.OrderBy(l => l.Date).ToList().ForEach(p => sb.AppendLine(string.Format("{0},{1}", p.Date.ToString("yyyy-MM-dd"), p.ClosePrice.ToString(".00"))));

                File.WriteAllText(Application.StartupPath + AppSettings.GetValue<string>("DailySavePath") + StockCode + ".txt", sb.ToString());
            }
            catch (Exception ex)
            {
                MessageBox.Show(string.Format("{0}（{2}） 出错：{1}\n", StockCode, ex.Message, StockName));
            }

            this.DialogResult = System.Windows.Forms.DialogResult.OK;
            this.Close();
        }

        private void frmPriceEdit_Load(object sender, EventArgs e)
        {
            this.lblStockName.Text = StockName;
            this.dateTimePicker1.Value = IsAdd ? DateTime.Today : Date;
            this.numericUpDown1.Value = Convert.ToDecimal(Price);
            if (!this.IsAdd)
            {
                this.dateTimePicker1.Enabled = false;
            }
        }
    }
}
